package com.cloud.wms.ui.inside.presenter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.config.SpKey;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.inside.contract.TransferDownContact;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.sort.entity.DetachBoxBean;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by FC on 2021/5/17.
 * Desc：
 */
public class TransferDownPresenter extends BaseRxPresenter<TransferDownContact.View> implements TransferDownContact.Presenter {
    public static final int CHECK_POS = 0x100;
    public static final int GET_COMMODITY_MSG = 0x101;
    public static final int CONFIRM_DOWN = 0x102;
    public static final int CHECK_BOX_SUCCESS = 0x103;

    @Inject
    public TransferDownPresenter() {

    }

    /**
     * 检测库位是否有效
     *
     * @param PosCode
     */
    @Override
    public void checkPos(String PosCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("PosCode", PosCode);
//        Observable.timer(500, TimeUnit.MILLISECONDS)
//                .map(new Function<Long, Boolean>() {
//                    @Override
//                    public Boolean apply(Long aLong) throws Exception {
//                        return true;
//                    }
//                })
        FineExApplication.component().http().transferDownCheckPos(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Integer>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Integer integer) {
                        mView.onSuccess(MessageCreator.createMessage(integer, CHECK_POS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 获取移库暂存库位或者容器商品信息
     *
     * @param barCode
     */
    @Override
    public void getCommodityMsg(String posID, String barCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("PosID", posID);
        params.put("barCode", barCode);
//        Observable.timer(500, TimeUnit.MILLISECONDS)
//                .map(new Function<Long, List<TransferCommodityEntity>>() {
//                    @Override
//                    public List<TransferCommodityEntity> apply(Long aLong) throws Exception {
//                        List<TransferCommodityEntity> entityList = new ArrayList<>();
//                        for (int i = 0; i < 3; i++) {
//                            TransferCommodityEntity entity = new TransferCommodityEntity();
//                            entity.setCommodityName(barCode);
//                            entity.setBarCode(barCode);
//                            entity.setStockType(i);
//                            entity.setPosID("1");
//                            entity.setPosCode("kw123");
//                            entity.setProductBatchID(String.valueOf(i));
//                            entity.setProductionDate("2021-4-1" + i);
//                            entity.setExpirationDate("2022-10-1" + i);
//                            entity.setInDate("2021-5-1" + i);
//                            entity.setAmount(100);
//                            entityList.add(entity);
//                        }
//                        return entityList;
//                    }
//                })
        FineExApplication.component().http().transferCommodityMsg(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<TransferCommodityEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<TransferCommodityEntity> entityList) {
                        mView.onSuccess(MessageCreator.createMessage(entityList, GET_COMMODITY_MSG));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 检测容器
     *
     * @param boxCode
     */
    @Override
    public void checkContainer(String boxCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("ContainerCode", boxCode);
//        Observable.timer(500, TimeUnit.MILLISECONDS)
//                .map(new Function<Long, DetachBoxBean>() {
//                    @Override
//                    public DetachBoxBean apply(Long aLong) throws Exception {
//                        DetachBoxBean boxBean = new DetachBoxBean();
//                        boxBean.setContainerCode(boxCode);
//                        boxBean.setContainerID(1);
//                        return boxBean;
//                    }
//                })
        FineExApplication
                .component()
                .http()
                .transferCheckContainer(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<DetachBoxBean>(mView.provideContext()) {
                    @Override
                    public void onSuccess(DetachBoxBean detachBoxBean) {
                        mView.onSuccess(MessageCreator.createMessage(detachBoxBean, CHECK_BOX_SUCCESS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 确认下架
     *
     * @param commodityEntity
     * @param amount
     */
    @Override
    public void confirmDown(TransferCommodityEntity commodityEntity, String amount, DetachBoxBean boxBean) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("memberID", commodityEntity.getMemberID());
        params.put("posID", commodityEntity.getPosID());
        params.put("commodityID", commodityEntity.getCommodityID());
        params.put("productBatchID", commodityEntity.getProductBatchID());
        params.put("stockType", commodityEntity.getStockType());
        params.put("amount", amount);
        if (FineExApplication.component().kv().decodeBool(SpKey.TRANSFER_SCAN_CONTAINER) && boxBean != null) {
            params.put("containerID", boxBean.getContainerID());
        }
//        Observable.timer(500, TimeUnit.MILLISECONDS)
//                .map(new Function<Long, Boolean>() {
//                    @Override
//                    public Boolean apply(Long aLong) throws Exception {
//                        return true;
//                    }
//                })
        FineExApplication.component().http().transferConfirmDown(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object aBoolean) {
                        mView.onSuccess(MessageCreator.createMessage(aBoolean, CONFIRM_DOWN));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
